package com.zkyc.framework.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.mapper.TemplateMapper;
import com.zkyc.framework.common.pojo.Template;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.service.TemplateService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;

/**
 * (Template)表服务实现类
 *
 * @author wj
 * @since 2023-02-02 13:51:03
 */
@Service("templateService")
public class TemplateServiceImpl implements TemplateService {
    @Resource
    private TemplateMapper templateMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Template queryById(Integer id) {
        return this.templateMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public ResultVO queryByPage(Integer type,Integer page,Integer size) {
        Page pageable = new Page(page, size);
        LambdaQueryWrapper<Template> wrapper = new LambdaQueryWrapper<Template>().eq(Template::getType, type);
        Page page1 = templateMapper.selectPage(pageable,wrapper);
        return ResultVO.data(page1);
    }

    /**
     * 新增数据
     *
     * @param template 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO insert(Template template) {
        if (template == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        templateMapper.insert(template);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 修改数据
     *
     * @param template 实例对象
     * @return 实例对象
     */
    @Override
    public ResultVO update(Template template) {
        if (template == null && template.getId() == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(template.getId()) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        templateMapper.updateById(template);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public ResultVO deleteById(Integer id) {
        if (id == null) {
            return ResultVO.fail(ResultCode.PARAM_INVALID);
        }
        if (queryById(id) == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        templateMapper.deleteById(id);
        return ResultVO.success(ResultCode.SUCCESS);
    }
}
